package com.photography.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.annotation.RequireRole;
import com.photography.common.Result;
import com.photography.entity.Carousel;
import com.photography.service.CarouselService;
import jakarta.annotation.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.List;


@RestController
@RequestMapping("/carousel")
public class CarouselController {
    @Resource
    private CarouselService carouselService;

    /**
     * 新增轮播图
     *
     * @param carousel 轮播图信息
     * @return 结果
     */
    @RequireRole("manager")
    @PostMapping("/add")
    public Result addCarousel(@RequestBody Carousel carousel) {
        return carouselService.addCarousel(carousel);
    }

    /**
     * 删除轮播图
     *
     * @param id 轮播图id
     * @return 结果
     */
    @RequireRole("manager")
    @DeleteMapping("/delete/{id}")
    public Result deleteCarousel(@PathVariable Integer id) {
        return carouselService.deleteCarousel(id);
    }

    /**
     * 更新轮播图信息
     *
     * @param carousel 轮播图信息
     * @return 结果
     */
    @RequireRole("manager")
    @PutMapping("/update")
    public Result updateCarousel(@RequestBody Carousel carousel) {
        return carouselService.updateCarousel(carousel);
    }

    /**
     * 分页查询轮播图
     *
     * @param current  当前页码
     * @param size     每页大小
     * @param category 类别
     * @return 结果
     */

    @GetMapping("/carouselPage")
    public Result getCarouselPage(@RequestParam(defaultValue = "1") Integer current,
                                  @RequestParam(defaultValue = "10") Integer size,
                                  @RequestParam(required = false) String category) {
        // 创建分页对象
        Page<Carousel> page = new Page<>(current, size);
        // 执行分页查询
        Page<Carousel> carouselPage = carouselService.getCarouselPage(page, category);
        return Result.success(carouselPage, "查询成功");
    }

    /**
     * 根据类别查询所有轮播图
     *
     * @param category 类别
     * @return 结果
     */
    @GetMapping("/carouselAll")
    public Result getCarouselAll(@RequestParam String category) {
        return carouselService.getCarouselAll(category);
    }

    /**
     * 根据分类查询轮播图
     *
     * @param category 分类名称
     * @return 结果
     */
    @GetMapping("/category/{category}")
    public Result getCarouselByCategory(@PathVariable String category) {
        return carouselService.getCarouselAll(category);
    }


}
